//
//  MovieListViewController.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "MovieDetailViewController.h"
#import "AppTools.h"
#import "Movie.h"

@interface MovieListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)NSMutableArray *movieArray;

@property (nonatomic, copy)NSString *movieID;

@property (nonatomic, retain)NSMutableDictionary *bigDic;

@end

@implementation MovieListViewController

- (void)dealloc
{
    [_movieTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.movieTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.movieTableView.delegate = self;
    self.movieTableView.dataSource = self;
    self.navigationItem.title = @"电影";
    [self.view addSubview:self.movieTableView];
    [self.movieTableView release];
    
    [self.movieTableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"movieCell"];
    
    self.movieTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.movieArray = [NSMutableArray array];
    
    //缓存
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 1) lastObject];
    NSString *dicPath = [document stringByAppendingPathComponent:@"/movieListDic.plist"];
    self.bigDic = [NSKeyedUnarchiver unarchiveObjectWithFile:dicPath];
    NSMutableArray *array = [self.bigDic objectForKey:@"result"];
    for (NSMutableDictionary *dic in array) {
        Movie *movie = [[Movie alloc] init];
        [movie setValuesForKeysWithDictionary:dic];
        [self.movieArray addObject:movie];
        [movie release];
    }

    
    [AppTools getDataWithURLStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" Block:^(id consequnce) {
        
        [NSKeyedArchiver archiveRootObject:consequnce toFile:dicPath];
        self.bigDic = consequnce;
        NSMutableArray *array = [self.bigDic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            Movie *movie = [[Movie alloc] init];
            [movie setValuesForKeysWithDictionary:dic];
            [self.movieArray addObject:movie];
            [movie release];
        }
        [self.movieTableView reloadData];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 150;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.movieArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellId = @"movieCell";
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
//    NSLog(@"++++++%@", movie.movieId);
    [cell sendValue:movie];
//    NSLog(@"%@",movie);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc] init];
    
    
//    NSLog(@"%@",self.movieID);
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    self.movieID = movie.movieId;
    
    movieDVC.movieId = self.movieID;

    
    [self.navigationController pushViewController:movieDVC animated:1];
    
    [self.movieTableView deselectRowAtIndexPath:indexPath animated:1];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
